<template>
  <el-dialog
    v-model="dialogVisible"
    title="新增模具型腔"
    width="1000px"
    @close="closeDialog"
  >
    <el-form
      ref="formRef"
      label-position="left"
      label-width="120px"
      :model="state.form"
      require-asterisk-position="right"
    >
      <el-form-item label="型腔编码" prop="cavityCode" :required="true">
        <el-input v-model="state.form.cavityCode" />
      </el-form-item>
      <el-form-item label="型腔名称">
        <el-input v-model="state.form.cavityName" />
      </el-form-item>
      <el-form-item label="型腔规格">
        <el-input v-model="state.form.cavitySpec" />
      </el-form-item>
      <el-form-item label="型腔备注">
        <el-input v-model="state.form.remark" />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
  import { addMoldCavityUrl } from '@/api/equip/mold_api'
  import { localStorageConfig } from '@/config/localstorage.config'
  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const $baseMessage = inject('$baseMessage')
  const dialogVisible = ref(false)
  const formRef = ref()
  const state = reactive({
    form: {
      pkMoldDoc: '',
      cavityCode: '',
      cavityName: '',
      cavitySpec: '',
      remark: '',
      pkCorp,
    },
  })
  const closeDialog = () => {
    dialogVisible.value = false
  }
  const save = () => {
    formRef.value.validate(async (valid) => {
      if (valid) {
        const { msg } = await addMoldCavityUrl(state.form)
        $baseMessage(msg, 'success')
        _emits('saveSuccess')
        closeDialog()
      }
    })
  }
  defineExpose({
    handleOpen: (pkMoldDoc) => {
      if (!pkMoldDoc) return
      state.form.pkMoldDoc = pkMoldDoc
      dialogVisible.value = true
    },
  })
  const _emits = defineEmits(['saveSuccess'])
</script>
<style lang="scss" scoped></style>
